package com.gxan.web.renshi;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Archives;
import com.gxan.renshiservice.ServiceArchives;
import com.gxan.renshiservice.impl.ServiceArchivesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/archives/findAllArchives")
public class FindAllArchivesServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        //获取当前条和分页数
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        ServiceArchives serviceArchives = new ServiceArchivesImpl();
        Map map = new HashMap();
        try {
            List<Archives> result = serviceArchives.findAllArchives(page,limit);
            map.put("code",0);
            map.put("msg","success");
            map.put("count",serviceArchives.countArchives());
            map.put("data",result);
            resp.getWriter().write(JSONObject.toJSONString(map));
        } catch (SQLException e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","系统在开小差，请联系管理员");
            resp.getWriter().write(JSONObject.toJSONString(map));
        }
    }
}
